package com.pds.web.maintenance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.web.maintenance.entity.PdsEnvironmentGuidanceStep;
import com.pds.web.maintenance.mapper.PdsEnvironmentGuidanceStepMapper;
import com.pds.web.maintenance.service.PdsEnvironmentGuidanceStepService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 规范指导步骤表
 *
 * @author srf sunlightcs@gmail.com
 * @since 1.0.0 2024-02-23
 */
@Service("pdsEnvironmentGuidanceStepService")
public class PdsEnvironmentGuidanceStepServiceImpl extends ServiceImpl<PdsEnvironmentGuidanceStepMapper, PdsEnvironmentGuidanceStep> implements PdsEnvironmentGuidanceStepService {
    @Resource
    private PdsEnvironmentGuidanceStepMapper pdsEnvironmentGuidanceStepMapper;

    @Override
    public List<PdsEnvironmentGuidanceStep> selectList(PdsEnvironmentGuidanceStep pdsEnvironmentGuidanceStep) {
        QueryWrapper<PdsEnvironmentGuidanceStep> wrapper = new QueryWrapper<>();
        Long guidanceId = pdsEnvironmentGuidanceStep.getGuidanceId();
        String stepName = pdsEnvironmentGuidanceStep.getStepName();
        if(!Objects.isNull(guidanceId)){
            wrapper.eq("guidance_id",guidanceId);
        }
        if(!Objects.isNull(stepName) && !stepName.isEmpty()){
            wrapper.like("step_name",stepName);
        }
        wrapper.orderByDesc("update_time").orderByDesc("create_time");
        return pdsEnvironmentGuidanceStepMapper.selectList(wrapper);
    }

    @Override
    public PdsEnvironmentGuidanceStep queryById(Long id) {
        return pdsEnvironmentGuidanceStepMapper.selectById(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public PdsEnvironmentGuidanceStep insert(PdsEnvironmentGuidanceStep pdsEnvironmentGuidanceStep) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsEnvironmentGuidanceStep.setCreateTime(date);
        pdsEnvironmentGuidanceStepMapper.insert(pdsEnvironmentGuidanceStep);
        return pdsEnvironmentGuidanceStep;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean update(PdsEnvironmentGuidanceStep pdsEnvironmentGuidanceStep) {
        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsEnvironmentGuidanceStep.setUpdateTime(date);
        return pdsEnvironmentGuidanceStepMapper.updateById(pdsEnvironmentGuidanceStep) > 0;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        return pdsEnvironmentGuidanceStepMapper.deleteById(id) > 0;
    }
}